<?php
/**
 * Created by PhpStorm.
 * User: caijianling
 * Date: 2018/8/17
 * Time: 下午2:29
 */

session_start();

if (!isset($_POST)) {
    exit('非法访问');
} elseif (!isset($_POST['type'])) {
    exit('非法访问');
}

require_once './admin-core/admin-function.php';
$admin_sql = new TOAdminSQL();
$ff = new fileFunctions();
require_once './admin-core/session.php';
$session = new Session();

switch ($_POST['type']) {
    case 'sign-up':
        if (isset($_POST['email']) && isset($_POST['name']) && isset($_POST['password']) && isset($_POST['password_again']) && isset($_POST['read'])) {
            $sign_up_res = $admin_sql->sign_up($_POST['email'], $_POST['name'], $_POST['password'], $_POST['password_again'], $_POST['read'], 2, 1);
            exit(json_encode($sign_up_res));
        } else exit(json_encode(['status' => 1, 'error' => 'don\'t have 6 items']));
        break;
    case 'login':
        if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['remember'])) {
            $login_res = $admin_sql->login($_POST['name'], $_POST['password'], $_POST['remember']);
            if ($login_res['status'] === 0) {
                $remember = 86400; // 3600*24
                if ($login_res['remember'] === 1) $remember = 1296000; // 86400*15
                $session::set('uid', $login_res['uid'], $remember);
            }
            exit(json_encode($login_res));
        } else exit(json_encode(['status' => 1, 'error' => 'don\'t have 4 items']));
        break;
    case 'sign-out':
        $session::clear('uid');
        exit(json_encode(['status' => 0]));
        break;
    case 'website-set':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $res_ws = $ff->config_variable_set('./admin-core/config.php', $_POST['json'], 'config_website_set', 11);
        exit(json_encode($res_ws));
        break;
    case 'about-set':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $res_ws = $ff->config_variable_set('./admin-core/config.php', $_POST['json'], 'about_us_set', 4);
        exit(json_encode($res_ws));
        break;
    case 'add-about':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->about_us_insert($_POST['json'], Session::get('uid'), 3);
        exit(json_encode($insert_res));
        break;
    case 'add-swipe':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->index_swipe_insert($_POST['json'], Session::get('uid'));
        exit(json_encode($insert_res));
        break;
    case 'add-index-news':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->index_news_insert($_POST['json'], Session::get('uid'));
        exit(json_encode($insert_res));
        break;
    case 'add-index-show':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->index_show_insert($_POST['json'], Session::get('uid'));
        exit(json_encode($insert_res));
        break;
    case 'index-auto-about':
        $res = $ff->set_auto_about();
        exit(json_encode($res));
        break;
    case 'index-about-set':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $res = $ff->config_variable_set('./admin-core/config.php', $_POST['json'], 'index_about_set', 4);
        exit(json_encode($res));
        break;
    case 'ajax_build_html':
        if (!isset($_POST['html'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $res = $ff->build_sth($_POST['html']);
        exit($res);
    case 'add-news':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->news_insert($_POST['json'], Session::get('uid'));
        exit(json_encode($insert_res));
        break;
    case 'ajax_search_php':

        break;
    case 'add-role':
        if (!isset($_POST['json'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->role_insert($_POST['json']);
        exit(json_encode($insert_res));
        break;
    case 'add-news-html':
        if (!isset($_POST['nid'])) exit(['status' => 1, 'error' => 'don\'t have 2 items']);
        $insert_res = $admin_sql->add_news_html($_POST['nid']);
        exit(json_encode($insert_res));
        break;
    default:
}